<template>
  <div>
    <template v-if="isFormRender">
      <form-render-vue ref="formRenderRef" @ok="ok" @cancel="cancel" />
    </template>
    <template v-else-if="isPathRender">
      <path-render-vue ref="pathRenderRef" @ok="ok" @cancel="cancel" />
    </template>
  </div>
</template>

<script>
import formRenderVue from "./formRender.vue";
import pathRenderVue from "./pathRender.vue";
import Modal from "ant-design-vue/lib/modal";
export default {
  provide() {
    const $modal = this;
    return {
      getOptions() {
        return $modal.paramOptions;
      },
      getParams() {
        return $modal.params;
      },
    };
  },
  components: {
    formRenderVue,
    pathRenderVue,
  },
  computed: {
    isFormRender() {
      return this.contentType === "form";
    },
    isPathRender() {
      return this.contentType === "path";
    },
  },
  data() {
    return {
      contentType: "",
      paramOptions: {},
      params: {},
    };
  },
  methods: {
    openModal(option = {}, params = {}) {
      this.params = params;
      this.loadOption(option);
    },
    closeModal() {},
    loadOption(option) {
      this.paramOptions = option;
      const { dialog = {} } = option;
      if (dialog.pageType == "path") {
        this.renderPath(dialog);
      } else if (dialog.pageType == "form") {
        if (dialog && dialog.formType === "custom") {
          this.renderForm(dialog);
        }
      } else {
        // 未配置表单 但是触发了表单按钮
        Modal.confirm({
          title: "提示",
          content:
            "当前流程按钮弹窗配置不完善，请联系管理员。是否继续当前流程？",
          okText: "确定",
          cancelText: "取消",
          onOk: () => {
            this.ok();
          },
          onCancel: () => {
            this.cancel();
          },
        });
      }
    },
    renderForm(option) {
      this.contentType = "form";
      const params = this.params;
      this.$nextTick(() => {
        this.$refs.formRenderRef &&
          this.$refs.formRenderRef.init(option, params);
      });
    },
    renderPath(option) {
      this.contentType = "path";
      this.$nextTick(() => {
        this.$refs.pathRenderRef && this.$refs.pathRenderRef.init(option);
      });
    },
    ok() {
      this.$emit("ok");
    },
    cancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped></style>